package ex.recruit.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.orm.Page;
import com.guinea.core.orm.PropertyFilter;
import com.guinea.core.service.EntityService;
import com.guinea.core.utils.ServletUtils;
import com.guinea.core.web.StrutsEntityAction;

import ex.recruit.service.RmProvinceServiceEx;
import ex.system.service.UserServiceEx;

public class RmProvinceActionEx extends StrutsEntityAction {
	RmProvinceServiceEx rmProvinceServiceEx;

	UserServiceEx userServiceEx;

	public EntityService getEntityService() {
		return rmProvinceServiceEx;
	}

	public RmProvinceServiceEx getRmProvinceServiceEx() {
		return rmProvinceServiceEx;
	}

	public void setRmProvinceServiceEx(RmProvinceServiceEx rmProvinceServiceEx) {
		this.rmProvinceServiceEx = rmProvinceServiceEx;
	}

	public UserServiceEx getUserServiceEx() {
		return userServiceEx;
	}

	public void setUserServiceEx(UserServiceEx userServiceEx) {
		this.userServiceEx = userServiceEx;
	}

	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map filterParamMap = ServletUtils.getParametersStartingWith(request,
				FILTER_PREFIX + "_");
		List filters = PropertyFilter.buildFromParaMap(filterParamMap);

		List managers = getRmProvinceServiceEx().findAndGroupManager(filters);

		request.setAttribute("managers", managers);
		request.setAttribute(FILTER_MAP, filterParamMap);
		return mapping.findForward(LIST);
	}

	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		bindForm(form, entity);

		return handleEdit(mapping, form, request, response);
	}

	protected ActionForward handleEntityMissing(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		saveError(request, "entity.missing");
		return list(mapping, form, request, response);
	}

	protected ActionForward handleEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List users = getUserServiceEx().getUsersByRoleName(
				"role_recruit_region_manager");
		request.setAttribute("regionManagers", users);
		if (isCreateOperation) {
			List provinces = (List) request.getSession().getServletContext()
					.getAttribute("provinces");
			List unallocatedProvinces = getRmProvinceServiceEx()
					.getUnallocatedProvinces(provinces);
			request.setAttribute("unallocatedProvinces", unallocatedProvinces);
		}
		return super.handleEdit(mapping, form, request, response);
	}

}
